using System;
using Jalal.Library.DataStructure;
using System.Collections.Generic;
using BCGC.DAL;
using Jalal.Library.Data;
using System.Data;
using System.Data.SqlTypes;
using Jalal.Library.Utility;
namespace BCGC.BLL.BusinessObject.News
{
    public class NewsItem
    {
        private int id;
        public int Id
        {
            get { return id; }
        }

        private String title;
        public String Title
        {
            get { return title; }
            set { title = value; }
        }

        private String description;
        public String Description
        {
            get { return description; }
            set { description = value; }
        }

        private DateTime createDateTime;
        public DateTime CreateDateTime
        {
            get { return createDateTime; }
        }

        private String shortDescription;
        public String ShortDescription
        {
            get { return shortDescription; }
            set { shortDescription = value; }
        }

        private DateTime lastUpdateDateTime;
        public DateTime LastUpdateDateTime
        {
            get { return lastUpdateDateTime; }
        }

        private bool isFeatured;
        public bool IsFeatured
        {
            get { return isFeatured; }
            set { isFeatured = value; }
        }

        private bool isActive;
        public bool IsActive
        {
            get { return isActive; }
            set { isActive = value; }
        }

        public NewsItem()
        {
            this.id = 0;
            this.description = string.Empty;
            this.title = string.Empty;
            this.shortDescription = string.Empty;
            this.createDateTime = SqlDateTime.MinValue.Value;
            this.isActive = false;
            this.isFeatured = false;
            this.lastUpdateDateTime = SqlDateTime.MinValue.Value;
        }

        public NewsItem(DataRow dataRow)
        {
            if (dataRow != null)
            {
                if (dataRow.Table.Columns.Contains("Id"))
                    this.id = (int)CommonUtility.FilterNull(dataRow["Id"], typeof(int));
                if (dataRow.Table.Columns.Contains("Title"))
                    this.title = (string)CommonUtility.FilterNull(dataRow["Title"], typeof(string));
                if (dataRow.Table.Columns.Contains("Description"))
                    this.description = (string)CommonUtility.FilterNull(dataRow["Description"], typeof(string));
                if (dataRow.Table.Columns.Contains("CreateDateTime"))
                    this.createDateTime = (DateTime)CommonUtility.FilterNull(dataRow["CreateDateTime"], typeof(DateTime));
                if (dataRow.Table.Columns.Contains("ShortDescription"))
                    this.shortDescription = (string)CommonUtility.FilterNull(dataRow["ShortDescription"], typeof(string));
                if (dataRow.Table.Columns.Contains("IsFeatured"))
                    this.isFeatured = (bool)CommonUtility.FilterNull(dataRow["IsFeatured"], typeof(bool));
                if (dataRow.Table.Columns.Contains("IsActive"))
                    this.isActive = (bool)CommonUtility.FilterNull(dataRow["IsActive"], typeof(bool));
                if (dataRow.Table.Columns.Contains("LastUpdateDateTime"))
                    this.lastUpdateDateTime = (DateTime)CommonUtility.FilterNull(dataRow["LastUpdateDateTime"], typeof(DateTime));
            }
            else
            {
                this.id = 0;
                this.description = string.Empty;
                this.title = string.Empty;
                this.shortDescription = string.Empty;
                this.createDateTime = SqlDateTime.MinValue.Value;
                this.isActive = false;
                this.isFeatured = false;
                this.lastUpdateDateTime = SqlDateTime.MinValue.Value;
            }
        }

        public static bool Create(string title, string shortDescription, string description, 
            bool isActive, bool isFeatured)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int id = NewsData.CreateNewNews(title, shortDescription, description,isActive, isFeatured);
            DataFactory.GetDataMiner().CommitTransaction();

            return (id > 0);
        }

        public static ReadWriteCustomCollection<NewsItem> GetAllNews(int currentPageIndex, 
            string sortOrder, int pageSize, string title, DateTime? createDateFrom, DateTime? createDateTo, 
            bool? isActive, bool? isFeatured, out int totalData)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);

            totalData = 0;

            DataTable newsData = NewsData.GetAllNews(currentPageIndex, sortOrder, pageSize, title,
                createDateFrom, createDateTo, isActive, isFeatured, out totalData);
            ReadWriteCustomCollection<NewsItem> news = LoadFromDataTable(newsData);

            DataFactory.GetDataMiner().CommitTransaction();

            return news;
        }

        private static ReadWriteCustomCollection<NewsItem> LoadFromDataTable(DataTable data)
        {
            ReadWriteCustomCollection<NewsItem> result = new ReadWriteCustomCollection<NewsItem>();

            if (data != null && data.Rows.Count > 0)
            {
                foreach (DataRow row in data.Rows)
                {
                    result.Add(new NewsItem(row));
                }
            }
            return result;
        }

        public static void Delete(List<int> newsToDelete)
        {
            DataFactory.GetDataMiner().BeginTransaction(false);

            try
            {
                int deletedItemCount = NewsData.DeleteNews(newsToDelete);
                if (deletedItemCount == newsToDelete.Count)
                    DataFactory.GetDataMiner().CommitTransaction();
                else
                    throw new Exception("Failed to delete all items");
            }
            catch (Exception ex)
            {
                DataFactory.GetDataMiner().RollBackTransaction();
                throw ex;
            }
        }

        public static void UpdateFeatured(List<int> featuredNews, 
            List<int> nonFeaturedNews)
        {
            DataFactory.GetDataMiner().BeginTransaction(false);

            try
            {
                int updatedItemCount = NewsData.UpdateFeaturedNews(featuredNews, 
                    nonFeaturedNews);
                if (updatedItemCount == featuredNews.Count + nonFeaturedNews.Count)
                    DataFactory.GetDataMiner().CommitTransaction();
                else
                    throw new Exception("Failed to update all items");
            }
            catch (Exception ex)
            {
                DataFactory.GetDataMiner().RollBackTransaction();
                throw ex;
            }
        }

        public static void UpdateActive(List<int> activeNews,
            List<int> inactiveNews)
        {
            DataFactory.GetDataMiner().BeginTransaction(false);

            try
            {
                int updatedItemCount = NewsData.UpdateActiveNews(activeNews,
                    inactiveNews);
                if (updatedItemCount == activeNews.Count + inactiveNews.Count)
                    DataFactory.GetDataMiner().CommitTransaction();
                else
                    throw new Exception("Failed to update all items");
            }
            catch (Exception ex)
            {
                DataFactory.GetDataMiner().RollBackTransaction();
                throw ex;
            }
        }

        public static ReadWriteCustomCollection<NewsItem> GetLatestNews(int howMany)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);

            DataTable newsData = NewsData.GetLatestNews(howMany);
            ReadWriteCustomCollection<NewsItem> news = LoadFromDataTable(newsData);

            DataFactory.GetDataMiner().CommitTransaction();

            return news;
        }

        public static NewsItem GetNewsItemById(int id)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            NewsItem newsItem = null;
            DataTable data = NewsData.GetNewsItemById(id);
            if (data != null && data.Rows.Count > 0)
                newsItem = new NewsItem(data.Rows[0]);

            DataFactory.GetDataMiner().CommitTransaction();

            return newsItem;
        }

        public static bool Update(int id, string title, string shortDescription, string description, 
            bool isActive, bool isFeatured)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int updateCount = NewsData.UpdateNews(id, title, shortDescription, description,
                isActive, isFeatured);
            DataFactory.GetDataMiner().CommitTransaction();

            return (updateCount > 0);
        }
    }
}
